"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Club_SocketMsgHandler = void 0;
const ErrorCode_1 = require("../src/config/ErrorCode");
const SocketConnectionManager_1 = require("../src/engine/socketserver/socket/SocketConnectionManager");
const DateUtil_1 = require("../src/engine/utils/DateUtil");
const jhaoproto_1 = require("../src/lib/net/protobuf/jhaoproto");
const ProtoManager_1 = require("../src/lib/net/protobuf/ProtoManager");
const ResponseModel_1 = require("../src/model/ResponseModel");
const ClubConfig_1 = require("./config/ClubConfig");
const ClubDao_1 = require("./dao/ClubDao");
const ClubUserDao_1 = require("./dao/ClubUserDao");
const RoomTemplateDao_1 = require("./dao/RoomTemplateDao");
const ClubEntity_1 = require("./entity/ClubEntity");
const ServerManager_1 = require("../src/server/ServerManager");
const RoomTemplateModel_1 = require("./model/RoomTemplateModel");
class Club_SocketMsgHandler {
    static onEventSocketGata(socketConnect, msg) {
        if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_list) {
            this.club_list(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_create) {
            this.club_create(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_join) {
            this.club_join(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_apply_list) {
            this.club_apply_list(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_apply_handle) {
            this.club_apply_handle(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_exit) {
            this.club_exit(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_member_list) {
            this.club_member_list(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_delete_member) {
            this.club_delete_member(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_member_actor) {
            this.club_member_actor(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_score) {
            this.club_score(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_create_room_template) {
            this.club_create_room_template(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_delete_room_template) {
            this.club_delete_room_template(socketConnect, msg);
        }
        else if (msg.cmdIndex == jhaoproto_1.JhaoProto.CmdIndex.club_room) {
            this.club_room(socketConnect, msg);
        }
    }
    static club_list(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let resultClubs = yield ClubDao_1.ClubDao.get_club_list(conn.user.userid);
            let respData = {
                clubs: resultClubs
            };
            conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_create, respData);
        });
    }
    static club_create(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_create, socketMsg.data);
            let clubid = yield ClubDao_1.ClubDao.randomClubId();
            let respModel = new ResponseModel_1.ResponseModel();
            if (clubid > 0) {
                let creator = conn.user.userid;
                let clubEntity = new ClubEntity_1.ClubEntity();
                clubEntity.club_name = protoPack.name;
                clubEntity.club_state = ClubConfig_1.ClubConfig.Club_State_Type.Normal;
                clubEntity.clubid = clubid;
                clubEntity.create_time = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
                clubEntity.creator = creator;
                clubEntity.notice = protoPack.notice;
                let isSuc = yield ClubDao_1.ClubDao.create_club(clubEntity);
                if (isSuc) {
                    respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                }
                else {
                    respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                    respModel.msg = "创建俱乐部数据库失败，稍后再试";
                }
            }
            else {
                respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                respModel.msg = "随机id失败，稍后再试";
            }
            if (respModel.code == ErrorCode_1.ErrorCode.Code.Suc) {
                this.club_list(conn, socketMsg);
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, respModel.code, respModel.msg);
            }
        });
    }
    static club_join(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_join, socketMsg.data);
            let respModel = yield ClubDao_1.ClubDao.join_club(conn.user.userid, protoPack.clubid);
            if (respModel.code == ErrorCode_1.ErrorCode.Code.Suc) {
                let respData = {};
                conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_join, respData);
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, respModel.code, respModel.msg);
            }
        });
    }
    static club_apply_list(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_apply_list, socketMsg.data);
            let users = yield ClubUserDao_1.ClubUserDao.req_club_apply_list(protoPack.clubid);
            let respData = {
                users: users
            };
            conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_apply_list, respData);
        });
    }
    static club_apply_handle(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_apply_handle, socketMsg.data);
            yield ClubUserDao_1.ClubUserDao.club_apply_handle(protoPack.id, protoPack.isAgree, conn.user.userid);
            let respData = {};
            conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_apply_handle, respData);
            if (protoPack.isAgree) {
                let notifyConn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(protoPack.userid);
                if (notifyConn) {
                    let resultClubs = yield ClubDao_1.ClubDao.get_club_list(protoPack.userid);
                    let respData = {
                        clubs: resultClubs
                    };
                    notifyConn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_list, 0, jhaoproto_1.JhaoProto.resp_club_list, respData);
                }
            }
        });
    }
    static club_exit(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_exit, socketMsg.data);
            let clubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid);
            if (clubUser) {
                clubUser.user_state = ClubConfig_1.ClubConfig.Club_merber_state_type.refuse;
                clubUser = yield ClubUserDao_1.ClubUserDao.update_user(clubUser);
                let respData = {};
                conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_exit, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_exit, respData);
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_member_list(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_member_list, socketMsg.data);
            let users = yield ClubUserDao_1.ClubUserDao.req_club_member_list(protoPack.clubid);
            let respData = {
                users: users
            };
            conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_member_list, respData);
        });
    }
    static club_delete_member(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_delete_member, socketMsg.data);
            let myClubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid);
            if (myClubUser) {
                if (myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.administrator) {
                    let clubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid);
                    if (clubUser) {
                        if (clubUser.actor != ClubConfig_1.ClubConfig.Club_Actor_Type.owner) {
                            clubUser.user_state = ClubConfig_1.ClubConfig.Club_merber_state_type.refuse;
                            clubUser = yield ClubUserDao_1.ClubUserDao.update_user(clubUser);
                            let respData = {};
                            conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_delete_member, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_delete_member, respData);
                        }
                        else {
                            conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "圈主不能踢出");
                        }
                    }
                    else {
                        conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
                    }
                }
                else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "您不是管理员");
                }
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_member_actor(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_member_actor, socketMsg.data);
            let myClubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid);
            if (myClubUser) {
                if (myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.administrator) {
                    let clubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid);
                    if (clubUser) {
                        if (clubUser.actor != ClubConfig_1.ClubConfig.Club_Actor_Type.owner) {
                            if (protoPack.actor != ClubConfig_1.ClubConfig.Club_Actor_Type.owner) {
                                clubUser.actor = protoPack.actor;
                                clubUser = yield ClubUserDao_1.ClubUserDao.update_user(clubUser);
                                let respData = {};
                                conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_member_actor, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_member_actor, respData);
                            }
                            else {
                                conn.sendErrorToClient(0, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不能设置为圈主");
                            }
                        }
                        else {
                            conn.sendErrorToClient(0, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "圈主不用设置管理员");
                        }
                    }
                    else {
                        conn.sendErrorToClient(0, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
                    }
                }
                else {
                    conn.sendErrorToClient(0, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "您不是管理员");
                }
            }
            else {
                conn.sendErrorToClient(0, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_score(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_score, socketMsg.data);
            let myClubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, conn.user.userid);
            if (myClubUser) {
                if (myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.owner || myClubUser.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.administrator) {
                    let clubUser = yield ClubUserDao_1.ClubUserDao.get_club_user(protoPack.clubid, protoPack.userid);
                    if (clubUser) {
                        clubUser.score = clubUser.score + protoPack.score;
                        clubUser = yield ClubUserDao_1.ClubUserDao.update_user(clubUser);
                        let respData = {};
                        conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_score, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_score, respData);
                    }
                    else {
                        conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
                    }
                }
                else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "您不是管理员");
                }
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_create_room_template(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_create_room_template, socketMsg.data);
            let user = conn.user;
            let club = yield ClubDao_1.ClubDao.get_club_simple(user.userid, protoPack.clubid);
            if (club) {
                let hadQuanXian = false;
                if (club.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.owner) {
                    hadQuanXian = true;
                }
                else if (club.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.administrator) {
                    hadQuanXian = true;
                }
                if (hadQuanXian) {
                    let dbSuc = yield RoomTemplateDao_1.RoomTemplateDao.insert_room_template(user.userid, protoPack);
                    if (dbSuc) {
                        ServerManager_1.ServerManager.instance.insertRoomTemplateModel();
                        let respData = {};
                        conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_create_room_template, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_create_room_template, respData);
                    }
                    else {
                        conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "插入房间模板时出错");
                    }
                }
                else {
                    conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "您没有此权限");
                }
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_delete_room_template(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_delete_room_template, socketMsg.data);
            let user = conn.user;
            let club = yield ClubDao_1.ClubDao.get_club_simple(user.userid, protoPack.clubid);
            if (club) {
                let hadQuanXian = false;
                if (club.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.owner) {
                    hadQuanXian = true;
                }
                else if (club.actor == ClubConfig_1.ClubConfig.Club_Actor_Type.administrator) {
                    hadQuanXian = true;
                }
                if (hadQuanXian) {
                    let dbSuc = yield RoomTemplateDao_1.RoomTemplateDao.delete_room_template(protoPack.id);
                    if (dbSuc) {
                        ServerManager_1.ServerManager.instance.deleteRoomTemplate(protoPack.id);
                        conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_delete_room_template, socketMsg.cmdOrder);
                    }
                    else {
                        conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "房间模板已经不存在");
                    }
                }
            }
            else {
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
    static club_room(conn, socketMsg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoManager_1.ProtoManager.instance.decode(jhaoproto_1.JhaoProto.req_club_room, socketMsg.data);
            let club = yield ClubDao_1.ClubDao.get_club_simple(conn.user.userid, protoPack.clubid);
            if (club) {
                let respData = {
                    template: RoomTemplateModel_1.RoomTemplateModel.getRoomTemplateListByClub(protoPack.clubid),
                };
                conn.createMsgToClient(jhaoproto_1.JhaoProto.CmdIndex.club_room, socketMsg.cmdOrder, jhaoproto_1.JhaoProto.resp_club_room, respData);
            }
            else {
                // conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Fail, "不在亲友圈", JhaoProto.CmdIndex.club_room)
                conn.sendErrorToClient(socketMsg.cmdOrder, jhaoproto_1.JhaoProto.ErrorCodeWs.Fail, "不在亲友圈");
            }
        });
    }
}
exports.Club_SocketMsgHandler = Club_SocketMsgHandler;
